<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 商品详情
 */

namespace app\api\controller\user\v1_0;



class Goods extends Common
{
    protected $noNeedRight = ['test'];
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 首页默认接口
     */    
    public function search(){
        $keyword = request()->param('keyword');
        $page = empty(request()->param('page')) ? 1 : request()->param('page');
        $limit = empty(request()->param('limit')) ? 10 : request()->param('limit');
        $where = [];
        if(!empty($keyword)){
            $where[] = ["goods.title","like","%{$keyword}%"];
        }
        $goods_list = (new \app\common\model\AgentGoodsSku())
                    ->alias("agent_goods_sku")
                    ->join("{$this->tdb}goods goods","goods.id = agent_goods_sku.goods_id")
                    ->field([
                        'agent_goods_sku.id',
                        'goods.img',
                        'goods.title',
                        'goods.introduce',
                        'agent_goods_sku.money',
                        'agent_goods_sku.money_primary',
                        'agent_goods_sku.stock',
                        'agent_goods_sku.team_rebate',
                        ])
                    ->append(['img'])
                    ->page($page)
                    ->limit($limit)
                    ->where($where)
                    ->where([
                        'agent_goods_sku.is_display' => 1,
                        'goods.is_display' => 1,
                    ])
                    ->where(['agent_goods_sku.agent_id' => $this->agent_id])
                    ->order('agent_goods_sku.sort desc')
                    ->select();
        return self::successResult("成功",$goods_list);
    }
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 商品详情
     */ 
    public function info(){
        $id = request()->param('id');
        $info = (new \app\common\model\AgentGoodsSku())
                        ->alias("agent_goods_sku")
                        ->join("{$this->tdb}goods goods","goods.id = agent_goods_sku.goods_id")
                        ->field([
                            'agent_goods_sku.id',
                            'goods.imgs',
                            'goods.img',
                            'goods.title',
                            'goods.introduce',
                            'goods.content',
                            'agent_goods_sku.money',
                            'agent_goods_sku.money_primary',
                            'agent_goods_sku.sales',
                            'agent_goods_sku.stock',
                            'agent_goods_sku.goods_id',
                            'agent_goods_sku.team_rebate',
                            ])
                        ->append(['imgs','content','stock'])
                        ->withAttr('sales',function($value,$data){
                            return (new \app\common\model\Goods())->where(['id' => $data['goods_id']])->value('x_sales')+$value;
                        })
                        ->where([
                            'agent_goods_sku.is_display' => 1,
                            'goods.is_display' => 1,
                        ])
                        ->where(['agent_goods_sku.agent_id' => $this->agent_id,'agent_goods_sku.id' => $id])
                        ->find();
      if(empty($info)){
        return self::errorResult("商品不存在");
      }
      $bus = (new \app\common\model\Bus())->where(['id' => $this->bus_id])->field(['id','nickname','avatar','address'])->find();
   
      return self::successResult("成功",[
        'goods' => $info,
        'bus' => $bus,
        'is_close' => aconfig('is_close',$this->agent_id),
        'user_info' => [
            'user_num' => $id,
            'goods_num' => $id*2,
            'list' => (new \app\common\model\UserRandom())->orderRaw("rand()")->limit(5)->select(),
        'share' => [
            'img' => $info['img'],
            'title' => $info['title']
        ],
      ]]);
    }
    public function buy_list(){
        $id = request()->param('id');
        $page = empty(request()->param('page')) ? 1 : request()->param('page');
        $limit = empty(request()->param('limit')) ? 10 : request()->param('limit');
        $info = (new \app\common\model\AgentGoodsSku())
                        ->alias("agent_goods_sku")
                        ->join("{$this->tdb}goods goods","goods.id = agent_goods_sku.goods_id")
                        ->field([
                            'agent_goods_sku.id',
                            'goods.img',
                            'goods.title',
                            ])
                     
                        ->where([
                            'agent_goods_sku.is_display' => 1,
                            'goods.is_display' => 1,
                        ])
                        ->where(['agent_goods_sku.agent_id' => $this->agent_id,'agent_goods_sku.id' => $id])
                        ->find();
        return self::successResult("成功",[
            'goods' => $info,
            'user_info' => [
                'user_num' => $id,
                'goods_num' => $id*2,
                'list' => [(new \app\common\model\UserRandom())->orderRaw("rand()")->page($page)->limit($limit)->select()]
            ],
            'share' => [
                'img' => $info['img'],
                'title' => $info['title']
            ]
            ]);
    }
    
}
